﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.ComponentModel;
using EIA.Basic.Model;

namespace EIA.SLClient.ProjectManage.Basic
{
    public class ProjectCreateViewModel : SimpleMvvmToolkit.ViewModelDetailBase<ProjectCreateViewModel, EIA.PM.Model.TProjectBasic>
    {
        public ProjectCreateViewModel()
        {
            IsShow = false;
            if (IsInDesignMode) IsShow = true;
            this.RegisterToReceiveMessages<EIA.PM.Model.TProjectBasic>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.PROJECT_NEW, EP_New);
        }

        private void EP_New(object sender, SimpleMvvmToolkit.NotificationEventArgs<EIA.PM.Model.TProjectBasic> e)
        {
            if (e.Data == null) Model = new PM.Model.TProjectBasic();
            else Model = e.Data;
            SendMessage(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.LOGIN_STAFF_CURRENT, new SimpleMvvmToolkit.NotificationEventArgs<object, TStaff>("", null, (s) =>
            {
                if (s != null)
                {
                    Model.Staff = s.StfId;
                    Model.PrjLeader = s.StfId;
                    Model.PrjLeaderName = s.StfName;
                }
            }));
            this.BeginEdit();
            IsShow = true;
        }

        private void Busy(string msg = "等待...")
        {
            IsBusy = true;
            SendMessage<bool>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>(msg, true));
        }

        private void Free()
        {
            IsBusy = false;
            SendMessage<bool>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>("", false));
        }

        #region notifyprop

        protected bool _IsBusy;
        /// <summary>
        /// 
        /// </summary>
        public bool IsBusy
        {
            get { return _IsBusy; }
            set
            {
                _IsBusy = value;
                NotifyPropertyChanged(m => m.IsBusy);
            }
        }

        protected string _TitleTip;
        /// <summary>
        /// 注释
        /// </summary>
        public string TitleTip
        {
            get { return _TitleTip; }
            set
            {
                _TitleTip = value;
                NotifyPropertyChanged(m => m.TitleTip);
            }
        }

        protected bool _IsShow;
        /// <summary>
        /// 是否显示
        /// </summary>
        public bool IsShow
        {
            get { return _IsShow; }
            set
            {
                _IsShow = value;
                NotifyPropertyChanged(m => m.IsShow);
            }
        }
        #endregion


        #region command

        #region SaveCommand

        private SimpleMvvmToolkit.DelegateCommand _SaveCommand = null;
        public SimpleMvvmToolkit.DelegateCommand SaveCommand
        {
            get
            {
                if (_SaveCommand == null)
                {
                    _SaveCommand = new SimpleMvvmToolkit.DelegateCommand(Save, CanExcuteSave);
                } return _SaveCommand;
            }
            set { _SaveCommand = value; NotifyPropertyChanged(m => m.SaveCommand); }
        }

        public void Save()
        {
            EIA.PM.BLL.ClientProxy.TProjectBasicProxy proxy = new PM.BLL.ClientProxy.TProjectBasicProxy();
            Busy();
            proxy.Insert(Model, (k) =>
            {
                Free();
                Model.PrjId = k;
                EndEdit();
                this.TitleTip = "";
                this.Model = null;
                this.IsShow = false;
            }, (ex) =>
            {
                Free();
                System.Windows.MessageBox.Show("登记项目发生错误", "提示", System.Windows.MessageBoxButton.OK);
            });
        }

        public bool CanExcuteSave()
        {
            return true;
        }

        #endregion

        #region CancelCommand

        private SimpleMvvmToolkit.DelegateCommand _CancelCommand = null;
        public SimpleMvvmToolkit.DelegateCommand CancelCommand
        {
            get
            {
                if (_CancelCommand == null)
                {
                    _CancelCommand = new SimpleMvvmToolkit.DelegateCommand(Cancel, CanExcuteCancel);
                } return _CancelCommand;
            }
            set { _CancelCommand = value; NotifyPropertyChanged(m => m.CancelCommand); }
        }

        public void Cancel()
        {
            this.TitleTip = "";
            this.CancelEdit();
            this.Model = null;
            this.IsShow = false;
        }

        public bool CanExcuteCancel()
        {
            return true;
        }

        #endregion

        #endregion
    }
}